var str=location.search.split('?')[1];
var id=str.split('&')[0];
var class_num=str.split('&')[1];
$('#me').attr('href','me.html?'+class_num);
var attach="";
var answer=null;
var sec="";
var sub="";
var homeworkfile="";
var isdeadline=false;
var issubmit=false;
if (islogin()) {

    //查询作业详情
    $.post({
        url:'../../requests/student/select_detail_homework.php',
        data:{'id':id},
        async:false,
        success:function (data) {
            $('#href_index').attr('href','course.html?'+data[0].class);
            $('#href_message').attr('href','message.html?'+data[0].class);
            sec=data[0].section;
            sub=data[0].subsection;
            $('#h-title').text(data[0].title);
            $('#h-notice').text(data[0].notice);
            if (data[0].deadline!='0000-00-00 00:00:00'){
                $('#h-deadline').text(data[0].deadline);

                if (checktime(data[0].deadline)) {
                   isdeadline=true;
                    var dom="<li class='item-content'><div class='item-inner'><div class='item-title'>已过期</div></div></li>";
                    $('ul').append(dom);
                }
                else {
                    isdeadline=false;
                }
            }
            else $('#h-deadline').text('无');

            if (data[0].section !="0") {
                $('#sec-num').text(data[0].section+".");
            }
            else $('#sec-num').text('无');

            if (data[0].subsection != "0") {
                $('#sub-num').text(data[0].subsection+".");
            }
            else $('#sub-num').text('无');

            if (data[0].attach == "") {
                $('#d-attach').text('无');
            }
            else attach="../../"+data[0].attach;

            if (data[0].answer==null) {
                $('#d-answer').text('无');
            }
            else answer="../../"+data[0].answer;

        }
    });

    if (sec != 0) {
        $.post({
            url:'../../requests/student/select_homework_sec.php',
            data:{'sec':sec,'id':id,'sub':sub},
            success:function (data) {
                $('#sec-name').text(data.sec);
                if (data.sub != "0") {
                    $('#sub-name').text(data.sub);
                }
            }
        });
    }

    //查询提交详情
    $.post({
        url:'../../requests/student/select_submit.php',
        data:{'id':id},
        async:false,
        success:function (data) {
            console.log(data[0]);
            if (data.length ==0) {
                //未提交显示提交，隐藏下载作业、答案
                isdeadline=false;
            }
            else if (data == 'error') {
                $$.alert('error');
            }
            else {
                //已经提交作业，显示下载作业、答案，隐藏提交作业
                issubmit=true;
                var dom="<li class='item-content'><div class='item-inner'><div class='item-title'>已提交作业</div></div></li>";
                $('ul').append(dom);
                homeworkfile="../../"+data[0].attach;
                $('#score').text(data[0].score);
            }
        }
    });

    //已提交或已过期，隐藏提交，显示下载答案和作业
    if (isdeadline||issubmit) {
        $('#btn_submit').hide();
        $("input[name='up-homeworkfile']").hide();
        $('#d-homeworkfile').show();
        $('#d-answer').show();
    }
    else {
        $('#btn_submit').show();
        $("input[name='up-homeworkfile']").show();
        $('#d-homeworkfile').hide();
        $('#d-answer').hide();

    }
    $('#d-homeworkfile').attr('href',homeworkfile);
    if (attach != "") {
        $('#d-attach').attr('href',attach);
    }
    if (answer != "") {
        $('#d-answer').attr('href',answer);
    }
    // 提交作业
    $('#btn_submit').click(function () {
        var fd=new FormData($('#form_up')[0]);
        var md=new Date();
        var time=format(md,'-');
        fd.append('homework',id);
        fd.append('time',time);
        $.post({
            url:'../../requests/student/submit_homework.php',
            data:fd,
            processData: false,
            contentType: false,
            success:function (data) {
                if (data == 1) {
                    location.reload(true);
                }
                else {
                    $$.alert('error',function () {
                        location.reload(true);
                    })
                }
            }
        });
    });
}
else window.location.href='../../index.html';
//检查作业是否过期
function checktime(deadline) {
    if (deadline == "0000-00-00 00:00:00") {
        return false;
    }
    else {
        var mydate = new Date();
        var time = format(mydate, '-');
        var check = false;
        time = new Date(time.replace(/\-/g, '\/'));
        deadline = new Date(deadline.replace(/\-/g, '\/'));
        if (time >= deadline) {
            check = true;
        }
        else check = false;
        return check;
    }

}
//格式化时间
function format(Date,str){
    var obj = {
        Y: Date.getFullYear(),
        M: Date.getMonth() + 1,
        D: Date.getDate(),
        H: Date.getHours(),
        Mi: Date.getMinutes(),
        S: Date.getSeconds()
    }
    // 拼接时间 hh:mm:ss
    var time = ' ' +supplement(obj.H) + ':' + supplement(obj.Mi) + ':' + supplement(obj.S);
    // yyyy-mm-dd
    if(str.indexOf('-') > -1){
        return obj.Y + '-' + supplement(obj.M) + '-' + supplement(obj.D) + time;
    }
    // yyyy/mm/dd
    if(str.indexOf('/') > -1){
        return obj.Y + '/' + supplement(obj.M) + '/' + supplement(obj.D) + time;
    }
}
// 位数不足两位补全0
function supplement(nn){
    return nn = nn < 10 ? '0' + nn : nn;
}